/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
// 使用哈希表
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p1 = headA,p2 = headB;
        Map<ListNode,Integer> map = new HashMap<>();
        while(p1 != null){
            map.put(p1,1);
            p1 = p1.next;
        }
        while(p2 != null){
            if(map.containsKey(p2)){
                return p2;
            }
            p2 = p2.next;
        }
        return null;
    }
}